package com.gdut.simple.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gdut.simple.R;
import com.gdut.simple.contract.NewsContract;
import com.gdut.simple.data.entity.News;
import com.gdut.simple.presenter.NewsPresenter;
import com.gdut.simple.view.activity.WebActivity;
import com.gdut.simple.view.adapter.NewsAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.basic.AbsBasicFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsFragment extends AbsBasicFragment implements NewsContract.View {

    @BindView(R.id.id_item_list_view)
    RecyclerView mNewsListView;
    @BindView(R.id.id_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private NewsAdapter mNewsAdapter;
    private List<News> mNewsList;
    private String type;
    private int mCurrentPage = 1;
    private NewsContract.Presenter mNewsPresenter;

    public static NewsFragment newInstance(String type) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString("type");
        new NewsPresenter(this);
    }

    @Override
    public void fetchData() {
        mNewsPresenter.refresh();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_refresh_list, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRefreshLayout();
        initRecyclerView();
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark),
                ContextCompat.getColor(getActivity(), R.color.colorPrimary),
                ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNewsPresenter.refresh();
            }
        });
    }

    private void initRecyclerView() {
        mNewsListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mNewsListView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        mNewsListView.setItemAnimator(new DefaultItemAnimator());
        mNewsList = new ArrayList<>();
        mNewsAdapter = new NewsAdapter(getContext(), mNewsList);
        mNewsListView.setAdapter(mNewsAdapter);
        mNewsAdapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "新闻资讯");
                bundle.putString("url", mNewsList.get(position).getUrl());
                bundle.putString("title", mNewsList.get(position).getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public boolean onLongClick(View view, int position) {
                return false;
            }
        });
        mNewsListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                if (lastVisibleItem >= totalItemCount - 1 && dy > 0) {
                    mNewsPresenter.loadMore(++mCurrentPage);
                }
            }
        });
    }

    @Override
    public List<News> getItemList() {
        return mNewsList;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public void reset() {
        mCurrentPage = 1;
    }

    @Override
    public void refreshList() {
        if (mNewsAdapter != null) {
            mNewsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showRefreshProgress() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideRefreshProgress() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void setPresenter(@NonNull NewsContract.Presenter presenter) {
        this.mNewsPresenter = presenter;
    }
}
